// pages/goodsDetail/goodsDetail.js
const util = require("../../utils/util.js")
const api = require("../../utils/net.js")
const wxUtils = require("../../utils/wxUtils.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    isLoading: true,
    commentRate: 100,
    commentNumber: 0,
    xToast: {
      show: false
    },
    pageNo: 1,
    pageSize: 4,
    hasMore: true,
    type: "",
    orderList: [],
    goodsId: 1,
    imageCount:0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that=this
    var goodsId = options.goodsId
    var commentNumber = options.commentNumber
    var commentRate = options.commentRate
    this.setData({
      goodsId: goodsId,
      commentRate: commentRate,
      commentNumber: commentNumber
    })
    this.getList();

    api.get("orderComment", {
      offset: 1,
      limit: 1,
      type: 1,
      goodsId: this.data.goodsId
    }, {
        onNext: (res) => {
         that.setData({
           imageCount:res.count
         }) 
        }
    })
  },
  changeType: function(event) {
    var type = event.currentTarget.dataset.type;
    this.setData({
      orderList: [],
      pageNo: 1,
      pageSize:4,
      hasMore: true,
      isLoading: true,
      type: type
    })

    this.getList();
  },
  preview: function(event) {
    var index = event.currentTarget.dataset.index;
    var images = event.currentTarget.dataset.images;

    wx.previewImage({
      current: images[index], // 当前显示图片的http链接
      urls: images // 需要预览的图片http链接列表
    })

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.setData({
      pageNo: this.data.pageNo + 1
    });
    this.getList();
  },

  /**
   * 所有列表
   */
  getList: function() {
    let that = this;
    let pageNo = this.data.pageNo;
    let pageSize = this.data.pageSize;
    let offset = pageSize * (pageNo - 1);
    api.get("orderComment", {
      offset: offset,
      limit: pageSize,
      type: this.data.type,
      goodsId: this.data.goodsId
    }, {
      onNext: (res) => {
        let orderList = [];

        res.rows.forEach((item, index) => {
          var array = new Array();
          if (!util.isEmptyStr(item.imageUrl)) {
            array = item.imageUrl.split(",");
            item.imageUrls = array;
          }
          orderList.push(item);
        });

        if (pageNo !== 1) {
          orderList = that.data.orderList.concat(orderList);
        }
        let hasMore = false;
        if (orderList.length < res.count) {
          hasMore = true;
        }
        that.setData({
          isLoading: false,
          total: res.count,
          orderList: orderList,
          hasMore: hasMore
        })
      },
      onError: (msg, code) => {
        wx.showToast({
          title: msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

})